﻿using Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public class BookInfoDAL
    {
        SqlCommand cmd;//定义SqlCommand对象
        public BookInfoDAL()
        {
            cmd = new SqlCommand();
            cmd.CommandType = CommandType.StoredProcedure;//使用存储过程访问数据库
        }
        public DataSet GetBookClass()
        {
            cmd.CommandText = "GetClass";//指定存储过程名称
            cmd.Parameters.Clear();//清除存储过程参数值

            DataSet ds = DBHelper.GetDataSet(cmd);
            return ds;
        }
        /// <summary>
        /// 数据库查重
        /// </summary>
        public bool CheckingDulication(BookInfo bo)
        {
            cmd.CommandText = "GetBookByBookID";
            cmd.Parameters.Clear();
            cmd.Parameters.Add("@BookID", SqlDbType.VarChar, 15).Value = bo.BookID;
            object obj = DBHelper.GetScalar(cmd);
            if (obj != null)
                return true;
            else
                return false;
        }
        /// <summary>
        /// 图书修改方法实现
        /// </summary>
        /// <param name="bo">BookInfo对象</param>
        /// <param name="operation">操作标记</param>
        /// <returns>布尔值</returns>
        public bool EditBook(BookInfo bo,string operation)
        {
            if (operation == "I")
                cmd.CommandText = "InsertNewBook"; //当操作值为"I"，进行新增操作
            if (operation == "U")
                cmd.CommandText = "UpdateBook"; //当操作值为"U"，进行更新操作
            cmd.Parameters.Clear();
            cmd.Parameters.Add("@BookID", SqlDbType.VarChar, 15).Value = bo.BookID;
            cmd.Parameters.Add("@BookName", SqlDbType.VarChar, 200).Value = bo.BookName;
            cmd.Parameters.Add("@ISBN", SqlDbType.Char, 20).Value = bo.ISBN;
            cmd.Parameters.Add("@Author", SqlDbType.VarChar, 50).Value = bo.Author;
            cmd.Parameters.Add("@Publisher", SqlDbType.VarChar, 40).Value = bo.Publisher;
            cmd.Parameters.Add("@BookVersion", SqlDbType.VarChar, 40).Value = bo.BookVersion;

            cmd.Parameters.Add("@WordCount", SqlDbType.Int).Value = bo.WordCount;
            cmd.Parameters.Add("@PageCount", SqlDbType.Int).Value = bo.PageCount;

            cmd.Parameters.Add("@ClassID", SqlDbType.Char,10).Value = bo.BookClass.BookClassID;

            try
            {
                int num = DBHelper.ExecuteSQL(cmd);
                if (num > 0)
                {
                    return true;
                }
                else
                    return false;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
